﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Subgurim.Controles;
using System.Data;
using System.Configuration;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Xml;
using System.Text;


public partial class Incident : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string incidentId = Request.QueryString["id"];  //Sets the Incident ID Number (used to query the Database) to the QueryString that was sent
        GMapCreate(); // Calls the function used to populate the Google Map Control
        resolve_btn.Attributes.Add("onclick", "return confirm('Are you sure you want to resolve this incident? This cannot be undone');");
        verify_btn.Attributes.Add("onclick", "return confirm('Are you sure you want to set this incident to Active? This cannot be undone');");
        deny_btn.Attributes.Add("onclick", "return confirm('Are you sure you want to resolve this incident? This cannot be undone');");
    }

    protected void editButton_Click(object sender, EventArgs e)
    {
        FormView1.ChangeMode(FormViewMode.Edit);
    }

    protected void GMapCreate() // This function is used to populate the information that will be shown on the Google Map Control.
    {
        string lm = ((Label)FormView1.FindControl("Landmarklabel")).Text;
        string state = ((Label)FormView1.FindControl("State_Namelabel")).Text;
        string city = ((Label)FormView1.FindControl("CityLabel")).Text;

        string dr = ((Label)FormView1.FindControl("Date_ReportedLabel")).Text;
        string des = ((Label)FormView1.FindControl("DescriptionLabel")).Text;

        string fullAddr = lm + ", " + city + ", " + state; //combines all the locations to give to the geocode requester.

        GMap1.Add(new GControl(GControl.preBuilt.LargeMapControl));
        GeoCode geographycode = GMap1.geoCodeRequest(fullAddr);
        GLatLng gLngLat = new GLatLng(geographycode.Placemark.coordinates.lat, geographycode.Placemark.coordinates.lng);
        //int zoom = Int32.Parse(GMapType.GTypes.Normal.ToString());
        GMap1.setCenter(gLngLat, 11);
        GMarker lm_marker = new GMarker(gLngLat);
        GMap1.addGMarker(lm_marker);
        GInfoWindow window = new GInfoWindow(lm_marker, "Location: " + lm + "<br /> Description: " +des+ "<br />Date Reported: " + dr);
        GMap1.addInfoWindow(window); 
    }

    protected void GridView1_databound(object sender, EventArgs e)//outstanding jobs
    {
        outstanding_jobs_Label.Text = GridView1.Rows.Count.ToString();
        if ((outstanding_jobs_Label.Text == "0") && (((Label)FormView1.FindControl("StatusLabel")).Text != "Verification"))
        {
            resolve_btn.Visible = true;
        }
    }

    protected void GridView2_databound(object sender, EventArgs e)//completed job
    {
        completed_jobs_label.Text = GridView2.Rows.Count.ToString();
    }


    protected void fmview_databound(object sender, EventArgs e)
    {
        string status = ((Label)FormView1.FindControl("StatusLabel")).Text;
        if (status == "Verification")
        {
            verify_btn.Visible = true;
            btn_newJob.Visible = false;
            deny_btn.Visible = true;
        }

        if (status == "Resolved")
        {
            btn_newJob.Visible = false;
            verify_btn.Visible = false;
            deny_btn.Visible = false;
            resolve_btn.Visible = false;
        }
    }
    
    protected void btn_newJob_Click(object sender, EventArgs e)
    {
        string value = Request.QueryString["id"];
        Response.Redirect("~/DMS_cnjob.aspx?ID=" + value);
    }

    protected void resolve_btn_Click(object sender, EventArgs e)
    {
        string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|Database.accdb;Persist Security Info=True";
        DateTime dt = DateTime.Parse("#" + DateTime.Now.ToShortDateString() + "#");

        OleDbConnection db_conn = new OleDbConnection(connString);
        OleDbCommand updateCom = new OleDbCommand("UPDATE Incidents SET Status = ?, [Date Resolved] = ? WHERE (Incident_ID = ?)", db_conn);

        updateCom.Parameters.Add("STATUS", "Resolved");
        updateCom.Parameters.Add("Date Resolved", dt);
        updateCom.Parameters.Add("INCIDENT_ID", Int32.Parse(Request.QueryString["id"]));
        db_conn.Open();
        updateCom.ExecuteNonQuery();
        db_conn.Close();

        Response.Redirect(Request.Url.ToString());

        btn_newJob.Visible = false;
                
    }

    protected void verify_btn_Click(object sender, EventArgs e)
    {
        string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|Database.accdb;Persist Security Info=True";

        OleDbConnection db_conn = new OleDbConnection(connString);
        OleDbCommand updateCom = new OleDbCommand("UPDATE Incidents SET Status = ? WHERE (Incident_ID = ?)", db_conn);

        updateCom.Parameters.Add("STATUS", "Active");
        updateCom.Parameters.Add("INCIDENT_ID", Int32.Parse(Request.QueryString["id"]));
        db_conn.Open();
        updateCom.ExecuteNonQuery();
        db_conn.Close();

        Response.Redirect(Request.Url.ToString());

        btn_newJob.Visible = true;
    }
    protected void deny_btn_Click(object sender, EventArgs e)
    {
        DateTime dt = DateTime.Parse("#" + DateTime.Now.ToShortDateString() + "#");

        string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|Database.accdb;Persist Security Info=True";

        OleDbConnection db_conn = new OleDbConnection(connString);
        OleDbCommand updateCom = new OleDbCommand("UPDATE Incidents SET Status = ?, [Date Resolved] = ? WHERE (Incident_ID = ?)", db_conn);

        updateCom.Parameters.Add("STATUS", "Denied");
        updateCom.Parameters.Add("Date Resolved", dt);
        updateCom.Parameters.Add("INCIDENT_ID", Int32.Parse(Request.QueryString["id"]));
        db_conn.Open();
        updateCom.ExecuteNonQuery();
        db_conn.Close();

        Response.Redirect(Request.Url.ToString());
    }
}